//
//  ReceiveTableViewCell.m
//  EasyChat
//
//  Created by romana on 16/6/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ReceiveTableViewCell.h"
#import "Masonry.h"


@implementation ReceiveTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self doIt];
    }
    return self;

}


-(void)doIt{

//    self.myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//    self.myLabel.backgroundColor = [UIColor clearColor];
//    self.myLabel.layer.masksToBounds = YES;
//    self.myLabel.layer.cornerRadius = 10;
//    self.myLabel.font = [UIFont systemFontOfSize:18];
//    self.myLabel.numberOfLines = 0;
   // [self.myLabel setNumberOfLines:0];
  
  
   // [self.contentView addSubview:self.myLabel];

    self.headImageView = [UIImageView new];
    self.headImageView.backgroundColor = [UIColor whiteColor];
      //  self.myLabel.layer.cornerRadius = 10;
    self.headImageView.image = [UIImage imageNamed:@"chatListHead"];
    [self.contentView addSubview:self.headImageView];
    
    self.backImageView = [UIImageView new];
    self.backImageView.backgroundColor = [UIColor whiteColor];
    self.backImageView.layer.cornerRadius = 10;
    [self.contentView addSubview:self.backImageView];
    
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.myLabel.backgroundColor = [UIColor clearColor];
    self.myLabel.layer.masksToBounds = YES;
    self.myLabel.layer.cornerRadius = 10;
    self.myLabel.font = [UIFont systemFontOfSize:18];
    self.myLabel.numberOfLines = 0;
    [self.contentView addSubview:self.myLabel];
  

}


-(void)layoutSubviews{

    [super layoutSubviews];
    self.headImageView.frame = CGRectMake(5, 5, 30, 30);
 
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18],NSFontAttributeName, nil];
    
    CGRect rect = [self.myLabel.text boundingRectWithSize:CGSizeMake(self.frame.size.width / 2 - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.myLabel.frame = CGRectMake(60 , 20, self.contentView.frame.size.width / 4 * 3 - 50, rect.size.height);

  self.myLabel.preferredMaxLayoutWidth = self.contentView.frame.size.width / 4 * 3 - 50;
    [self.myLabel sizeToFit];
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.mas_equalTo(self.myLabel).offset(-10);
        make.right.mas_equalTo(self.myLabel).offset(10);
        make.bottom.mas_equalTo(self.myLabel).offset(-10);
        
    }];

}

//-(CGFloat)CellHeight{
//    
//    //重新布局
//    [self layoutIfNeeded];
//    
//    return 40 + self.myLabel.bounds.size.height;
//    
//    
//}

-(void)setMessage:(EMMessage *)message{

    id body = message.messageBodies[0];
    if ([body isKindOfClass:[EMTextMessageBody class]]) {//文本消息
        EMTextMessageBody *textBody = body;
        self.myLabel.text = textBody.text;
    }else if([body isKindOfClass:[EMVoiceMessageBody class]]){//语音消息
        self.myLabel.text = @"[语音]";
    }else
    {self.myLabel.text = @"未知类型";
    }



}



@end
